package gqz.mvvm.view;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import gqz.mvvm.R;
import gqz.mvvm.utils.SharedPreferenceUtils;

/**
 * Created by Administrator on 2018/1/11.
 */

public class PunchActivity extends Activity {

    private final String TAG = "PunchActivity≡≡==--";//  »  ›  ▒

    private TextView tv, tv_content;
    //    private ListView lv;
    private Button btn;
    private ScrollView sv;

    private SimpleAdapter adapter;

    private int punchCount;//打卡次数
    private ArrayList<String> datalist = new ArrayList<>();//listview数据源

    private Handler handler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_punch);
        initView();
        initData();
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.punch_tv);
        tv_content = (TextView) findViewById(R.id.punch_tv_content);
//        lv = (ListView) findViewById(R.id.punch_lv);
        btn = (Button) findViewById(R.id.punch_btn);
        btn.setOnClickListener(onclick);
        sv = (ScrollView) findViewById(R.id.punch_sv);
    }

    private void initData() {
        punchCount = (int) SharedPreferenceUtils.getData(this, "count", 0);
        tv.setText(punchCount + "");
        if (punchCount > 0) {
            tv_content.setText("");
            for (int i = 0; i < punchCount; i++) {
//                datalist.add((i + 1) + ". " + SharedPreferenceUtils.getData(this, "" + i, "未获取到数据"));
                if (i != 0) tv_content.append("\n");
                tv_content.append((i + 1) + ". " + SharedPreferenceUtils.getData(this, "" + i, "未获取到数据"));
            }
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                sv.fullScroll(ScrollView.FOCUS_DOWN);
            }
        });
    }

    private void punch() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日-HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        SharedPreferenceUtils.setData(this, "" + punchCount, str);
        SharedPreferenceUtils.setData(this, "count", punchCount + 1);
    }

    View.OnClickListener onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            punch();
            initData();
        }
    };
}
